//
//  SLAlertController.m
//  ArchitectDemo
//
//  Created by 姚胜龙 on 2017/7/5.
//  Copyright © 2017年 姚胜龙. All rights reserved.
//

#import "SLAlertController.h"
#import "BuilderParams.h"
#import "Builder.h"

@interface SLAlertController ()

@property (nonatomic, strong) BuilderParams *params;

@end

@implementation SLAlertController

- (void)dealloc {
    NSLog(@"SLAlertController销毁了");
}

- (instancetype)initWithParams:(BuilderParams *)params {
    self = [super init];
    if (self) {
        self.params = params;
    }
    return self;
}

- (void)showAlert {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:self.params.title message:self.params.message preferredStyle:(UIAlertControllerStyleAlert)];
    if (![self isNilString:self.params.confirmTitle]) {
        [alert addAction:[UIAlertAction actionWithTitle:self.params.confirmTitle style:(UIAlertActionStyleDestructive) handler:self.params.confirmHandler]];
    }

    if (![self isNilString:self.params.cancelTitle]) {
        [alert addAction:[UIAlertAction actionWithTitle:self.params.cancelTitle style:(UIAlertActionStyleCancel) handler:self.params.cancelHandler]];
    }
    [self.params.context presentViewController:alert animated:YES completion:nil];
}

+(Builder *)construt:(UIViewController *)context {
    return [[Builder alloc] initWithContext:context];
}

#pragma mark - privite method

- (BOOL)isNilString:(NSString *)aString {
    if ([aString isKindOfClass:[NSNull class]]
        || aString.length <= 0
        || [aString isEqualToString:@""])
    {
        return YES;
    }
    return NO;
}

@end
